<?php
/**
 * Object Relational Persistence Mapping Class for table: ward
 *
 * @package	com.clearhealth
 * @author	ClearHealth Inc.
 */
class Ward extends ORDataObject {

	/**#@+
	 * Fields of table: ward mapped to class members
	 */
	var $ward_id			= '';
	var $vi_name			= '';
	var $en_name			= '';
	var $district_id		= '';
	var $auto_suggest_code 	= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'ward';

	/**
	 * Primary Key
	 */
	var $_key = 'ward_id';
	
	

	/**
	 * Internal Name
	 */
	var $_internalName = 'Ward';

	/**
	 * Handle instantiation
	 */
	function Ward() {
		parent::ORDataObject();
	}

	// Added by P.P.Dong, Oct 24, 2008
	//TODO: localization
	public function getWardList($district_id=0){
		$ds=& new Datasource_sql();
		$sql=array(
			'cols'=>"ward_id, name",
			'from'=>"$this->_table",
			'orderby' => 'ward_id'
		);
		if($district_id > 0) {
			$sql['where'] = "district_id = $district_id";
		}
		$ds->setup($this->_db, $sql);
		return $ds->toArray('ward_id','name');
	}
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	//TODO: localization
	public function getAllWards($districtId){
		$field	=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field, "name");
		$ds =& new Datasource_sql();
		$sql = array(
			'cols' 	=> "ward_id as ward_id, $field as name, auto_suggest_code",
			'from' => "$this->_table",
			'where' => "district_id = $districtId",
			'orderby' => 'name'
			);

		$ds->setup($this->_db,$sql);
		return $ds->toArray();
	}
	
	public function getSuggestedWardCode($nationalityId, 
	    $needle,  $limit, $short = true) {
	        	    
	    $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "ward_id, auto_suggest_code",
            'from' => "$this->_table",
            'where' => "auto_suggest_code LIKE '" . $needle . "%'",
            'orderby' => 'auto_suggest_code',
            'limit' => $limit
            );

        $ds->setup($this->_db,$sql);
        return $ds->toArray();
	    
	}
	
    public function getAreaInfo($wardId) {
        $field  =   L10nHelper::getLocale();
        $field	=	L10nHelper::changeField($field,"name");
        
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "ward_id, w.$field ward_name, d.district_id, d.$field district_name, p.province_id, p.$field province_name",
            'from' => "$this->_table w 
            INNER JOIN district d ON (w.district_id=d.district_id AND w.ward_id='$wardId')
            INNER JOIN province p ON (d.province_id=p.province_id)"
        );
        $ds->setup($this->_db,$sql);
        $result = $ds->toArray();
        if (empty($result)) {
           return array();
        }
        return $result[0];
        
    }
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	//get Ward by ward_id
	public function getWard($wardId){
		$field	=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field,"name");
		$sql = array(
			'cols' => "ward_id, $field as name, auto_suggest_code as code",
			'from' => "$this->_table",
			'where' => "ward_id = '$wardId'");
		$ds = & new Datasource_sql();
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) return false;
			else return $result[0];
	}
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	public function addWard($districtId, $name, $code){
	    $locales = L10nHelper::getSupportedLocales();
		$data = array();
       	foreach ( $locales as $lo => $la ) {
       		$lo	=	L10nHelper::changeField($lo,"name");
       		$data[$lo] = $name;
       	}
		$data['auto_suggest_code'] = $code;
		$data['district_id'] = $districtId;
		
		$this->populateArray($data);
		$this->persist();		
	}
	/**
	 * @author hdviet
	 * update for localize demography
	 */
	public function updateWard($wardId, $name, $code){
		$field		=	L10nHelper::getLocale();
		$field	=	L10nHelper::changeField($field,"name");
		$data = array(
			$field => $name,
			'auto_suggest_code' => $code
		);
        $this->setup($wardId);
        $this->populateArray($data);        
        $this->persist();
	}
	/**
	 * @author hdviet
	 * update for localize demography
	 */

	public function checkDuplicateName($name, $code, $districtId){
		$field	=	L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		
		$sql = array(
			'cols' 	=> "$field as name",
			'from' 	=> "$this->_table",
			'where' => "$field='$name'
			         AND auto_suggest_code = '$code'
			         AND district_id = '$districtId' ");
		$ds = & new Datasource_sql();
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) return false;
		foreach ( $result as $value ) {
			if (strcmp($name,$value['name']) == 0) {				
				return true;
			}       
		}		
		return false;
	}

}
?>
